<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/8/17
 * Time: 16:54
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;
use think\Validate;

class OrderPalace extends BaseValidate
{
    /**
     * 订单列表验证规则
     * @var array
     */
        protected  $rule=[
          'products'=>'checkProducts',
            'w_id'=>'require'
        ];
    /**
     * 订单列表中的商品验证规则
     * @var array
     */
        protected $singleRule = [
          'product_id'=>'require|isPositiveInt',
            'count'=>'require|isPositiveInt'
        ];
    /**
     * 传递商品验证规则
     * @param $values
     * @return bool
     * @throws ParameterException
     */
        protected function checkProducts($values){
            if (empty($values)){
                throw new ParameterException([
                   'msg'=>'商品不能为空'
                ]);
            }
            if (!is_array($values)){
                throw new ParameterException([
                   'msg'=>'商品参数不正确'
                ]);
            }
            foreach ($values as $value){
                $this->checkProduct($value);
            }
            return true;
        }
    /**
     * 单个商品验证规则,手动调用
     * @param $product
     * @return bool
     * @throws ParameterException
     */
        protected function checkProduct($product){
            $validate  = new BaseValidate($this->singleRule);
            $result = $validate->check($product);
            if (!$result){
                throw new ParameterException([
                    'msg'=>'商品参数不正确'
                ]);
            }
            return true;
        }
}